package com.example.project10;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

public class RegisterActivity extends AppCompatActivity {
    private EditText editTextEmail;
    private EditText editTextPassword;
    private EditText editTextConfirmPassword;
    private Button buttonRegister;
    private TextView tvLogin;
    private ProgressBar progressBar;
    private ImageView ivPasswordToggle;
    private ImageView ivConfirmPasswordToggle;
    private boolean isPasswordVisible = false;
    private boolean isConfirmPasswordVisible = false;
    
    private LoginViewModel loginViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        
        initViews();
        setupListeners();
        
        // 初始化ViewModel
        loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
        observeViewModel();
    }
    
    private void initViews() {
        editTextEmail = findViewById(R.id.editText_email);
        editTextPassword = findViewById(R.id.editText_password);
        editTextConfirmPassword = findViewById(R.id.editText_confirm_password);
        buttonRegister = findViewById(R.id.button_register);
        tvLogin = findViewById(R.id.tv_login);
        progressBar = findViewById(R.id.progressBar);
        ivPasswordToggle = findViewById(R.id.iv_password_toggle);
        ivConfirmPasswordToggle = findViewById(R.id.iv_confirm_password_toggle);
    }
    
    private void setupListeners() {
        buttonRegister.setOnClickListener(v -> {
            final String email = editTextEmail.getText().toString().trim();
            final String password = editTextPassword.getText().toString().trim();
            final String confirmPassword = editTextConfirmPassword.getText().toString().trim();
            
            if (email.isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请输入邮箱", Toast.LENGTH_SHORT).show();
                return;
            }
            if (password.isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (confirmPassword.isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请确认密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!password.equals(confirmPassword)) {
                Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            
            // 执行注册
            loginViewModel.register(email, password);
        });
        
        tvLogin.setOnClickListener(v -> {
            // 返回登录页面
            finish();
        });
        
        ivPasswordToggle.setOnClickListener(v -> {
            isPasswordVisible = !isPasswordVisible;
            if (isPasswordVisible) {
                editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                ivPasswordToggle.setImageResource(R.drawable.ic_eye_visible);
            } else {
                editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                ivPasswordToggle.setImageResource(R.drawable.ic_eye_hidden);
            }
            editTextPassword.setSelection(editTextPassword.getText().length());
        });
        
        ivConfirmPasswordToggle.setOnClickListener(v -> {
            isConfirmPasswordVisible = !isConfirmPasswordVisible;
            if (isConfirmPasswordVisible) {
                editTextConfirmPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                ivConfirmPasswordToggle.setImageResource(R.drawable.ic_eye_visible);
            } else {
                editTextConfirmPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                ivConfirmPasswordToggle.setImageResource(R.drawable.ic_eye_hidden);
            }
            editTextConfirmPassword.setSelection(editTextConfirmPassword.getText().length());
        });
    }
    
    private void observeViewModel() {
        loginViewModel.isLoading.observe(this, isLoading -> {
            if (isLoading != null) {
                if (isLoading) {
                    progressBar.setVisibility(View.VISIBLE);
                    buttonRegister.setEnabled(false);
                } else {
                    progressBar.setVisibility(View.GONE);
                    buttonRegister.setEnabled(true);
                }
            }
        });

        loginViewModel.registerResult.observe(this, result -> {
            if (result != null) {
                if (result.equals("SUCCESS")) {
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    // 注册成功后返回登录页面并传递用户信息
                    Intent resultIntent = new Intent();
                    resultIntent.putExtra("email", editTextEmail.getText().toString().trim());
                    setResult(RESULT_OK, resultIntent);
                    finish();
                } else if (result.startsWith("ERROR: ")) {
                    String errorMessage = result.substring(7); // 移除"ERROR: "前缀
                    Toast.makeText(RegisterActivity.this, errorMessage, Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}